/*
 * Cuadrado.cpp
 *
 *  Created on: 01/10/2013
 *      Author: ezequiel
 */

#include "Cuadrado.h"

Cuadrado::Cuadrado() {
	this->cube_index_buffer = 0;
	this->cube_index_buffer_size = 0;
	this->cube_normal_buffer = 0 ;
	this->cube_vertex_buffer = 0 ;
	this->cube_normal_buffer_size = 0 ;
	this->cube_vertex_buffer_size = 0 ;
	textureNormalId = 0 ;
//	this->tipoTexture = GL_TEXTURE0;
//	this->crearShaders("src/TexturingAndDiffuseShadingVShader.vert", "src/TexturingAndDiffuseShadingFShader.frag");
//	this->loadShader(GL_VERTEX_SHADER,"vertexCuadrado.vert");
//	this->loadAndInitTexture("arena.jpg");
	this->crearForma();

}

Cuadrado::~Cuadrado() {
    delete this->cube_vertex_buffer;
    delete this->cube_normal_buffer;
    delete this->cube_index_buffer;
    delete this->cube_texture_buffer;
}

void Cuadrado::render(GLuint handle, glm::mat4* matModelo) {
	 // Normal Matrix
	this->renderMatrix(handle, &proyeccion);



	glBindTexture (GL_TEXTURE_2D, this->textureid);

	// Bind Light Settings
		// ###################

	glm::vec3 light_intensity = glm::vec3(0.0f, 0.0f, 0.0f);
	glm::vec4 light_position = glm::vec4(0.0f, 0.0f, 200.0f, 0.0f);
	glm::vec3 La = glm::vec3(0.5f, 0.5f, 0.5f);
	glm::vec3 Ld = glm::vec3(1.0f, 1.0f, 1.0f);
	glm::vec3 Ls = glm::vec3(0.1f, 0.1f, 0.6f);
	glm::vec3 Ka = glm::vec3(0.3f, 0.3f, 0.3f);
	glm::vec3 Kd = glm::vec3(0.5f, 0.5f, 0.5f);
	glm::vec3 Ks = glm::vec3(1.0f, 1.0f, 1.0f);
	float Shininess = 0.5;

		// Light Intensity
		GLuint location_light_intensity = glGetUniformLocation(handle,
			"LightIntensity");

		if(location_light_intensity >= 0)
			glUniform3fv( location_light_intensity, 1, &light_intensity[0]);

		// Light Position
		GLuint location_light_position = glGetUniformLocation(handle,
			"LightPosition");

		if(location_light_position >= 0)
			glUniform4fv( location_light_position, 1, &light_position[0]);

		// La
		GLuint location_la = glGetUniformLocation(
			handle, "La");

		if(location_la >= 0)
			glUniform3fv( location_la, 1, &La[0]);

		// Ld
		GLuint location_ld = glGetUniformLocation(
			handle, "Ld");

		if(location_ld >= 0)
			glUniform3fv( location_ld, 1, &Ld[0]);

		// Ls
		GLuint location_ls = glGetUniformLocation(
			handle, "Ls");

		if(location_ls >= 0)
			glUniform3fv( location_ls, 1, &Ls[0]);


		// Ka
		GLuint location_ka = glGetUniformLocation(
			handle, "Ka");

		if(location_ka >= 0)
			glUniform3fv( location_ka, 1, &Ka[0]);

		// Kd
		GLuint location_kd = glGetUniformLocation(
			handle, "Kd");

		if(location_kd >= 0)
			glUniform3fv( location_kd, 1, &Kd[0]);

		// Ks
		GLuint location_ks = glGetUniformLocation(
			handle, "Ks");

		if(location_ks >= 0)
			glUniform3fv( location_ks, 1, &Ks[0]);


		// Shininess
		GLfloat location_shininess = glGetUniformLocation(handle,
			"Shininess");

		if(location_shininess >= 0)
			glUniform1f(location_shininess, Shininess);

	    glm::mat3 normal_matrix = glm::mat3 ( 1.0f );

	    // Bind Normal MAtrix
	    GLuint location_normal_matrix = glGetUniformLocation(handle, "NormalMatrix");
	    if( location_normal_matrix >= 0 )
		{
	        glUniformMatrix3fv( location_normal_matrix, 1, GL_FALSE, &normal_matrix[0][0]);
		}

	    // Bind Model Matrix
	    GLuint location_model_matrix = glGetUniformLocation( handle, "ModelMatrix");
	    if( location_model_matrix >= 0 )
		{
			glUniformMatrix4fv( location_model_matrix, 1, GL_FALSE,&((*matModelo)[0][0]));
		}

	   int loc = glGetUniformLocation(handle, "Tex1");
	    if( loc >= 0 )
	    {
			// We indicate that Uniform Variable sampler2D "text" uses  Texture Unit 0
	        glUniform1i(loc, 0);
	    }
	    else
	    {
	        fprintf(stderr, "Uniform variable Tex1 not found!\n");
	    }


	    if(this->textureNormalId != 0){
	    glActiveTexture(GL_TEXTURE1);
	    glEnable(GL_TEXTURE_2D);
	    int loc1 = glGetUniformLocation(handle, "NormalMapTex");
	    glUniform1i(loc1, 1);
	    glBindTexture(GL_TEXTURE_2D, this->textureNormalId);  }

	    glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_NORMAL_ARRAY);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glVertexPointer(3, GL_FLOAT, 0, cube_vertex_buffer);
		glNormalPointer(GL_FLOAT, 0, cube_normal_buffer);
		glTexCoordPointer(2, GL_FLOAT, 0, cube_texture_buffer);
	    glDrawElements(GL_QUADS, this->cube_index_buffer_size, GL_UNSIGNED_INT, this->cube_index_buffer);

	    glDisableClientState(GL_VERTEX_ARRAY);
	    glDisableClientState(GL_NORMAL_ARRAY);
	    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

void Cuadrado::render(GLuint handle) {
}

void Cuadrado::crearForma() {

    if (this->cube_vertex_buffer != NULL)
    {
        delete this->cube_vertex_buffer;
    }
    this->cube_vertex_buffer_size = 3*8;
    this->cube_vertex_buffer = new GLfloat[this->cube_vertex_buffer_size];
    this->cube_texture_buffer = new GLfloat[this->cube_vertex_buffer_size * 2 / 3 ];
    if (this->cube_normal_buffer != NULL)
    {
        delete this->cube_normal_buffer;
    }
    this->cube_normal_buffer_size = 3*8;
    this->cube_normal_buffer = new GLfloat[this->cube_normal_buffer_size];

    if (this->cube_index_buffer != NULL)
    {
        delete this->cube_index_buffer;
    }
    this->cube_index_buffer_size = 4 * 6;
    this->cube_index_buffer = new GLuint[this->cube_index_buffer_size];

    this->cube_vertex_buffer[0] = 0.5f;
    this->cube_vertex_buffer[1] = 0.5f;
    this->cube_vertex_buffer[2] = -0.5f;

    this->cube_vertex_buffer[3] = 0.5f;
    this->cube_vertex_buffer[4] = -0.5f;
    this->cube_vertex_buffer[5] = -0.5f;

    this->cube_vertex_buffer[6] = -0.5f;
    this->cube_vertex_buffer[7] = -0.5f;
    this->cube_vertex_buffer[8] = -0.5f;

    this->cube_vertex_buffer[9] = -0.5f;
    this->cube_vertex_buffer[10] = 0.5f;
    this->cube_vertex_buffer[11] = -0.5f;

    this->cube_vertex_buffer[12] = 0.5f;
    this->cube_vertex_buffer[13] = 0.5f;
    this->cube_vertex_buffer[14] = 0.5f;

    this->cube_vertex_buffer[15] = 0.5f;
    this->cube_vertex_buffer[16] = -0.5f;
    this->cube_vertex_buffer[17] = 0.5f;

    this->cube_vertex_buffer[18] = -0.5f;
    this->cube_vertex_buffer[19] = -0.5f;
    this->cube_vertex_buffer[20] = 0.5f;

    this->cube_vertex_buffer[21] = -0.5f;
    this->cube_vertex_buffer[22] = 0.5f;
    this->cube_vertex_buffer[23] = 0.5f;

    this->cube_index_buffer[0] = 0;
    this->cube_index_buffer[1] = 1;
    this->cube_index_buffer[2] = 2;
    this->cube_index_buffer[3] = 3;

    this->cube_index_buffer[4] = 4;
    this->cube_index_buffer[5] = 5;
    this->cube_index_buffer[6] = 6;
    this->cube_index_buffer[7] = 7;

    this->cube_index_buffer[8] = 0;
    this->cube_index_buffer[9] = 4;
    this->cube_index_buffer[10] = 5;
    this->cube_index_buffer[11] = 1;

    this->cube_index_buffer[12] = 1;
    this->cube_index_buffer[13] = 5;
    this->cube_index_buffer[14] = 6;
    this->cube_index_buffer[15] = 2;

    this->cube_index_buffer[16] = 2;
    this->cube_index_buffer[17] = 6;
    this->cube_index_buffer[18] = 7;
    this->cube_index_buffer[19] = 3;

    this->cube_index_buffer[20] = 3;
    this->cube_index_buffer[21] = 7;
    this->cube_index_buffer[22] = 4;
    this->cube_index_buffer[23] = 0;

//    this->cube_normal_buffer[0] = 0.5f;
//    this->cube_normal_buffer[1] = 0.5f;
//    this->cube_normal_buffer[2] = 0.0f;
//
//    this->cube_normal_buffer[3] = 0.5f;
//    this->cube_normal_buffer[4] = -0.5f;
//    this->cube_normal_buffer[5] = 0.0f;
//
//    this->cube_normal_buffer[6] = -0.5f;
//    this->cube_normal_buffer[7] = -0.5f;
//    this->cube_normal_buffer[8] = 0.0f;
//
//    this->cube_normal_buffer[9] = -0.5f;
//    this->cube_normal_buffer[10] = 0.5f;
//    this->cube_normal_buffer[11] = 0.0f;
//
//    this->cube_normal_buffer[12] = 0.5f;
//    this->cube_normal_buffer[13] = 0.5f;
//    this->cube_normal_buffer[14] = 0.0f;
//
//    this->cube_normal_buffer[15] = 0.5f;
//    this->cube_normal_buffer[16] = -0.5f;
//    this->cube_normal_buffer[17] = 0.0f;
//
//    this->cube_normal_buffer[18] = -0.5f;
//    this->cube_normal_buffer[19] = -0.5f;
//    this->cube_normal_buffer[20] = 0.0f;
//
//    this->cube_normal_buffer[21] = -0.5f;
//    this->cube_normal_buffer[22] = 0.5f;
//    this->cube_normal_buffer[23] = 0.0f;

        this->cube_normal_buffer[0] = 0.0f;
        this->cube_normal_buffer[1] = 0.0f;
        this->cube_normal_buffer[2] = -1.0f;

        this->cube_normal_buffer[3] = 0.0f;
        this->cube_normal_buffer[4] = 0.0f;
        this->cube_normal_buffer[5] = -1.0f;

        this->cube_normal_buffer[6] = 0.0f;
        this->cube_normal_buffer[7] = 0.0f;
        this->cube_normal_buffer[8] = -1.0f;

        this->cube_normal_buffer[9] = 0.0f;
        this->cube_normal_buffer[10] = 0.0f;
        this->cube_normal_buffer[11] = -1.0f;

        this->cube_normal_buffer[12] = 0.0f;
        this->cube_normal_buffer[13] = 0.0f;
        this->cube_normal_buffer[14] = 1.0f;

        this->cube_normal_buffer[15] = 0.0f;
        this->cube_normal_buffer[16] = 0.0f;
        this->cube_normal_buffer[17] = 1.0f;

        this->cube_normal_buffer[18] = 0.0f;
        this->cube_normal_buffer[19] = 0.0f;
        this->cube_normal_buffer[20] = 1.0f;

        this->cube_normal_buffer[21] = 0.0f;
        this->cube_normal_buffer[22] = 0.0f;
        this->cube_normal_buffer[23] = 1.0f;

    for (int i = 0 ; i < 15 ; i = i +8){
    	cube_texture_buffer[i] = 0.0f;
    	cube_texture_buffer[i+1] = 0.0f;
    	cube_texture_buffer[i+2] = 1.0f;
    	cube_texture_buffer[i+3] = 0.0f;
    	cube_texture_buffer[i+4] = 1.0f;
    	cube_texture_buffer[i+5] = 1.0f;
    	cube_texture_buffer[i+6] = 0.0f;
    	cube_texture_buffer[i+7] = 1.0f;
    }
//    cube_texture_buffer[] = {     0.0f,  0.0f,
//    	    1.0f,  0.0f,
//    	    1.0f,  1.0f,
//    	    0.0f,  1.0f,
//
//    	    0.0f,  0.0f,
//    	    1.0f,  0.0f,
//    	    1.0f,  1.0f,
//    	    0.0f,  1.0f,  };

}

void Cuadrado::activarTexture() {
}
